<template>
  <!-- 这里是歌单 -->
  <div class="playlist-wrap">
    <div class="playlist">
      <div class="classify_title flex">
        <div class="classify_title_left">
          <p>{{ $route.query.cat }}</p>
          <a href="javascript:;" class="menu_btn" @click="isHide = !isHide">
            <span
              >选择分类
              <em></em>
            </span>
          </a>
          <div class="cateListBox" :class="{ hide: isHide }">
            <div class="cateListBox_title">
              <div class="cateListBox_title_icn"></div>
            </div>
            <div class="cateListBox_bg cateListBox_body">
              <h3>
                <a href="javascript:;">
                  <span @click="changeList('全部')">全部风格</span>
                </a>
              </h3>
              <dl class="f-nb">
                <dt>
                  <i class="u-icn u-icn-1"></i>
                  语种
                </dt>
                <dd>
                  <span v-for="item in langArr" :key="item.name">
                    <a href="#" class="s-fc1" @click="changeList(item.name)">{{
                      item.name
                    }}</a>
                    <span class="cateListBox_line">|</span>
                  </span>
                </dd>
              </dl>
              <dl class="f-nb">
                <dt>
                  <i class="u-icn u-icn-2"></i>
                  风格
                </dt>
                <dd>
                  <span v-for="item in styleArr" :key="item.name">
                    <a href="#" class="s-fc1" @click="changeList(item.name)">{{
                      item.name
                    }}</a>
                    <span class="cateListBox_line">|</span>
                  </span>
                </dd>
              </dl>
              <dl class="f-nb">
                <dt>
                  <i class="u-icn u-icn-3"></i>
                  场景
                </dt>
                <dd>
                  <span v-for="item in sceneArr" :key="item.name">
                    <a href="#" class="s-fc1" @click="changeList(item.name)">{{
                      item.name
                    }}</a>
                    <span class="cateListBox_line">|</span>
                  </span>
                </dd>
              </dl>
              <dl class="f-nb">
                <dt>
                  <i class="u-icn u-icn-4"></i>
                  情感
                </dt>
                <dd>
                  <span v-for="item in emoArr" :key="item.name">
                    <a href="#" class="s-fc1" @click="changeList(item.name)">{{
                      item.name
                    }}</a>
                    <span class="cateListBox_line">|</span>
                  </span>
                </dd>
              </dl>
              <dl class="f-nb">
                <dt>
                  <i class="u-icn u-icn-5"></i>
                  主题
                </dt>
                <dd class="ddLast">
                  <span v-for="item in topicArr" :key="item.name">
                    <a href="#" class="s-fc1" @click="changeList(item.name)">{{
                      item.name
                    }}</a>
                    <span class="cateListBox_line">|</span>
                  </span>
                </dd>
              </dl>
            </div>
            <div class="cateListBox_bg cateListBox_ft"></div>
          </div>
        </div>
        <div class="classify_title_right">
          <a href="#">热门</a>
        </div>
      </div>
      <!-- 下面的歌单 -->
      <ul class="classify_song flex">
        <li
          v-for="(item, index) in playlist"
          :key="item.id"
          @click="$router.push(`/playlistDetail?id=${item.id}`)"
        >
          <div class="songD">
            <a href="javascript:;">
              <div class="msk_img"></div>
              <img :src="item.coverImgUrl" alt="" />
            </a>
            <div class="songD_bottom">
              <div class="songD_bottom_con flex">
                <div class="songD_bottom_con_ep flex">
                  <div class="songD_bottom_con_ep_icon"></div>
                  <p class="nb">{{ item.playCount | format }}</p>
                </div>
                <div class="songD_bottom_con_play pull-right">
                  <a href="#"></a>
                </div>
              </div>
            </div>
          </div>
          <a href="#">
            <p class="song_desc f-thide">
              {{ item.name }}
            </p>
          </a>
          <p class="by">
            by
            <a href="javascript:;" class="f-thide">{{
              item.creator.nickname
            }}</a
            ><img :src="identityIconUrl[index]" alt="" />
          </p>
        </li>
      </ul>
      <!-- 这里是分页组件 -->
      <div class="paginationDiv flex">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="totalData"
          :current-page="currentPages"
          :page-size="playlistData.limit"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getSongList, getSongClassifyApi } from "@/http/api.js";

export default {
  data() {
    return {
      isHide: true,
      playlist: [],
      // 当前页
      currentPage: localStorage.getItem("curPage"),
      playlistData: {
        // 分类
        cat: this.$store.state.cat,
        // 请求的条数
        limit: 35,
        // 偏移量
        offset: 0,
      },
      identityIconUrl: [],
      // 语种分类
      langArr: [],
      // 风格分类
      styleArr: [],
      // 场景分类
      sceneArr: [],
      // 情感分类
      emoArr: [],
      // 主题分类
      topicArr: [],
      //总数据
      totalData: 0,
    };
  },
  computed: {
    currentPages() {
      // 判断是否有curPage
      // 如果没有
      if (!parseInt(localStorage.getItem("curPage"))) {
        // 让currentPage为1
        return 1;
      }
      // 如果有,currentPages = 本地的curPage
      return parseInt(localStorage.getItem("curPage"));
    },
  },
  methods: {
    async getPlayList() {
      let cat = this.$route.query.cat;
      let limit = this.playlistData.limit;
      let offset =
        this.playlistData.limit * (localStorage.getItem("curPage") - 1);
      const params = {
        cat,
        limit,
        offset,
      };
      const res = await getSongList(params);
      this.totalData = res.total;
      this.playlist = res.playlists;
      res.playlists.forEach((el) => {
        if (!el.creator.avatarDetail) return this.identityIconUrl.push("");
        this.identityIconUrl.push(el.creator.avatarDetail.identityIconUrl);
      });
    },
    async getSongClassify() {
      const res = await getSongClassifyApi();
      res.sub.forEach((el) => {
        if (el.category == 0) this.langArr.push(el);
        if (el.category == 1) this.styleArr.push(el);
        if (el.category == 2) this.sceneArr.push(el);
        if (el.category == 3) this.emoArr.push(el);
        if (el.category == 4) this.topicArr.push(el);
      });
    },
    changeList(cat) {
      this.isHide = true;
      this.$router.push(`/playlist?cat=${cat}`);
      this.$store.commit("changeCate", cat);
      // this.getPlayList();
    },
    handleCurrentChange(val) {
      localStorage.setItem("curPage", val);
      this.getPlayList();
    },
  },
  created() {
    this.getPlayList();
    this.getSongClassify();
  },
  watch: {
    "$route.query": {
      handler() {
        this.getPlayList();
      },
      deep: true,
    },
  },
};
</script>
 
<style lang="less" scoped>
.playlist-wrap {
  width: 982px;
  margin: 0 auto;
  min-height: 700px;
  background-color: #fff;
  border: 1px solid #d3d3d3;
  border-top: 0;
  border-bottom: 0;

  .playlist {
    padding: 40px;
    position: relative;

    .classify_title {
      width: 100%;
      height: 42px;
      border-bottom: 2px solid #c10d0c;
      justify-content: space-between;
    }

    .classify_title_left p {
      margin: 0;
      font-weight: 400;
      font-size: 24px;
      display: inline-block;
    }

    .classify_title_right a {
      width: 46px;
      height: 29px;
      line-height: 29px;
      text-align: center;
      display: inline-block;
      color: #fff;
    }

    .classify_title_right a:hover {
      color: #fff;
    }

    .classify_title_right > a {
      width: 46px;
      height: 29px;
      margin: 0;
      font-size: 12px;
      line-height: 29px;
      color: #fff;
      border-radius: 3px;
      background: url(../../assets/images/button.png) no-repeat;
      background-position: 0 0;
      text-align: center;
    }

    .classify_title_left .menu_btn {
      width: 91px;
      height: 31px;
      font-size: 12px;
      font-weight: normal;
      margin: 2px 0 0 12px;
      display: inline-block;
      line-height: 31px;
      color: #0c73c2;
      background: url(../../assets/images/button2.png) no-repeat;
      background-position: right -100px;

      text-align: center;
    }

    .classify_title_left .menu_btn:hover {
      text-decoration: none;
      color: #0c73c2;
    }

    .classify_title_left .menu_btn span {
      color: #0c73c2;
      background: url(../../assets/images/button2.png) no-repeat;
      background-position: 0 -59px;
      display: inline-block;
      width: 90%;
    }

    .classify_title_left .menu_btn em {
      display: inline-block;
      width: 8px;
      height: 5px;
      background: url(../../assets/images/icon.png) no-repeat;
      background-position: -70px -543px;
      margin-left: 5px;
    }

    .cateListBox {
      position: absolute;
      top: 75px;
      left: 0;
      z-index: 5;
      width: 715px;
    }

    .cateListBox_bg {
      background: url(../../assets/images/sltlyr.png);
    }

    .cateListBox_title {
      width: 100%;
      height: 32px;
      background: url(../../assets/images/sltlyr.png) no-repeat;
    }

    .cateListBox_title_icn {
      position: absolute;
      top: 2px;
      left: 132px;
      display: inline-block;
      width: 24px;
      height: 11px;
      vertical-align: middle;
      background: url(../../assets/images/iconall.png) no-repeat;
      background-position: -48px 0;
    }

    .cateListBox_body {
      width: 100%;
      padding: 0 0 0 10px;
      background-position: -720px 0;
      background-repeat: repeat-y;
    }

    .cateListBox_ft {
      height: 20px;
      background-position: -1440px -12px;
    }

    .cateListBox_body h3 {
      margin: 0;
      height: 40px;
      padding-left: 26px;
      border-bottom: 1px solid #e6e6e6;
      font-weight: normal;
    }

    .cateListBox_body h3 a {
      color: #000;
      display: inline-block;
      width: 75px;
      height: 26px;
      text-align: center;
      line-height: 26px;
      background: url(../../assets/images/button.png) no-repeat;
      background-position: 0 -64px;
      font-size: 12px;
    }

    .f-nb dt {
      float: left;
      display: inline;
      width: 70px;
      margin: 0 -100px 0 26px;
      padding-top: 15px;
      font-weight: bold;
      font-size: 12px;
    }

    dl {
      margin: 0;
    }

    .f-nb dl {
      margin: 0;
    }

    .f-nb dd {
      margin-left: 96px;
      padding: 16px 15px 0 15px;
      border-left: 1px solid #e6e6e6;
      line-height: 24px;
    }

    .f-nb dd a {
      white-space: nowrap;
    }

    .f-nb:after {
      clear: both;
      content: ".";
      display: block;
      height: 0;
      visibility: hidden;
    }

    .u-icn {
      background: url(../../assets/images/icon.png) no-repeat;
      display: inline-block;
      overflow: hidden;
      vertical-align: middle;
      margin-right: 8px;
      margin-bottom: 4px;
    }

    .u-icn-1 {
      width: 23px;
      height: 23px;
      background-position: -20px -735px;
    }

    .u-icn-2 {
      width: 24px;
      height: 24px;
      background-position: 0 -60px;
    }

    .u-icn-3 {
      width: 24px;
      height: 24px;
      background-position: 0 -88px;
    }

    .u-icn-4 {
      width: 24px;
      height: 24px;
      background-position: 0 -117px;
    }

    .u-icn-5 {
      width: 24px;
      height: 24px;
      background-position: 0 -141px;
    }

    .cateListBox_line {
      margin: 0 8px 0 10px;
      color: #d8d8d8;
      font-size: 12px;
    }

    .s-fc1 {
      color: #333;
      font-size: 12px;
    }

    .f-nb5 dd {
      padding-bottom: 25px;
    }
  }

  .classify_song {
    /* margin-top: 30px; */
    margin: 30px 0 0 -50px;
    flex-wrap: wrap;
  }

  .classify_song li {
    display: inline-block;
    width: 140px;
    height: 188px;
    overflow: hidden;
    padding: 0 0 30px 50px;
    line-height: 1.4;
    box-sizing: content-box;
  }

  .classify_song li a {
    color: #000;
  }

  .songD {
    width: 140px;
    height: 140px;
    position: relative;
  }

  .songD img {
    width: 140px;
    height: 140px;
  }

  .msk_img {
    width: 140px;
    height: 140px;
    position: absolute;
    z-index: 1;
    background: url(../../assets/images/coverall.png) 0px 0px no-repeat;
  }

  .songD_bottom {
    width: 140px;
    height: 27px;
    position: absolute;
    background: url(../../assets/images/coverall.png) 0px -537px no-repeat;
    bottom: 0;
    z-index: 2;
  }

  .songD_bottom_con {
    height: inherit;
    padding: 0 10px 0 0;
    justify-content: space-between;
  }

  .songD_bottom_con_ep {
    height: inherit;
  }

  .songD_bottom_con_ep_icon {
    width: 14px;
    height: 11px;
    background: url(../../assets/images/iconall.png) 0px -24px no-repeat;
    margin: 9px 5px 6px 10px;
  }

  .songD_bottom_con_ep > p {
    margin: 0;
    padding-left: 5px;
    line-height: 30px;
    font-size: 12px;
    color: #ccc;
  }

  .songD_bottom_con_play {
    width: 16px;
    height: 17px;
    margin-top: 5px;
    background: url(../../assets/images/iconall.png) no-repeat;
  }

  .songD_bottom_con_play > a {
    display: block;
    height: inherit;
  }

  .songD_bottom_con_play > a:hover {
    background: url(../../assets/images/iconall.png) 0px -60px no-repeat;
  }

  .song_desc {
    margin: 8px 0 3px;
    font-size: 14px;
  }

  p.by img {
    height: 13px;
    width: 13px;
    display: inline-block;
    vertical-align: middle;
  }

  p.by {
    font-size: 12px;
    color: #999;
    margin-right: 5px;
  }

  p.by a {
    display: inline-block;
    color: #666;
    max-width: 70%;
    padding-right: 5px;
    vertical-align: bottom;
  }

  #star_img {
    width: 13px;
    height: 13px;
  }
}
/deep/ .paginationDiv {
  justify-content: center;
  height: 22px;
  line-height: 22px;
  .el-pagination.is-background .btn-next,
  .el-pagination.is-background .btn-prev,
  .el-pagination.is-background .el-pager li {
    height: 22px;
    line-height: 22px;
  }
  .el-pagination.is-background .el-pager li:not(.disabled) {
    color: #999;
    background-color: #fff;
    border: 1px solid #999;
  }
  .el-pagination.is-background .el-pager li:not(.disabled):hover {
    color: #999;
    background-color: #fff;
    border: 1px solid #999;
    cursor: pointer;
  }
  // 选中的样式
  .el-pagination.is-background .el-pager li:not(.disabled).active {
    background: url("../../assets/images/button.png") no-repeat 0 9999px;
    background-position: 0 -650px;
    border-color: #a2161b;
    color: #fff;
    cursor: default;
  }
  .el-pager .more::before {
    line-height: 22px;
  }
  .el-pagination.is-background .btn-next,
  .el-pagination.is-background .btn-prev {
    background: url("../../assets/images/button.png") no-repeat;
    width: 47px;
    padding: 0;
    padding-left: 22px;
    height: 22px;
    line-height: 24px;
    color: #333;
    text-align: left;
    box-sizing: content-box;
    vertical-align: middle;
    border: 1px solid #ccc;
  }

  .el-pagination.is-background .btn-prev {
    background-position: 0 -560px;
  }
  .el-pagination.is-background .btn-next {
    padding: 0;
    padding-right: 22px;
    text-align: right;
    background-position: -75px -560px;
  }
  .el-pagination.is-background .btn-prev:disabled {
    background-position: 0 -620px;
    color: #cacaca;
  }
  .el-pagination.is-background .btn-next:disabled {
    background-position: -75px -620px;
    color: #cacaca;
  }
  .el-pagination.is-background .btn-prev::before,
  .el-pagination.is-background .btn-prev:disabled::before {
    content: "上一页";
  }
  .el-pagination.is-background .btn-next::before,
  .el-pagination.is-background .btn-next:disabled {
    content: "下一页";
  }
  .el-pagination .btn-next .el-icon,
  .el-pagination .btn-prev .el-icon {
    display: none;
  }
}
</style>